package com.pomelo.tiku.frgs

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.pomelo.tiku.R
import com.pomelo.tiku.activities.AnswerQuestionActivity
import com.pomelo.tiku.activities.LoginActivity
import com.pomelo.tiku.adapters.HomeVacationAdapter
import com.pomelo.tiku.constants.SharePreferenceConstants
import com.pomelo.tiku.managers.SharedPreferenceManager
import com.pomelo.tiku.utils.ClickUtils
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*

class FragmentHome : BaseFragment(),View.OnClickListener {
    private var mView: View? = null

    private var list_vacation: ArrayList<String>? = null
    private var adapter_vacation: HomeVacationAdapter? = null
    private var isLogin = false

    private var question_pattern = ""

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mView = inflater.inflate(R.layout.fragment_home, container, false)
        return mView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    fun initView(view: View) {
        initUserInfo()
        initVacationRv(view)
        if(list_vacation == null) {
            view.tv_vacation.text = "您的职业： 0"
        } else {
            view.tv_vacation.text = "您的职业： ${list_vacation?.size}"
        }

        initEvent(view)
    }

    private fun initUserInfo() {
        isLogin = SharedPreferenceManager.getInstance(this.context).get(SharePreferenceConstants.KEY_ISLOGIN,false) as Boolean
    }

    private fun initEvent(view: View) {
        view.btn_anwser_question.setOnClickListener(this)
    }

    private fun initVacationRv(view: View) {
        list_vacation = ArrayList()
        list_vacation!!.add("Java后端工程师")
        list_vacation!!.add("会计师")
        list_vacation!!.add("前端工程师")
        adapter_vacation = HomeVacationAdapter(this.context,list_vacation)
        val layoutManager = LinearLayoutManager(this.context, RecyclerView.HORIZONTAL,false)
        view.rv_vacation.layoutManager = layoutManager
        view.rv_vacation.adapter = adapter_vacation
        adapter_vacation!!.setOnItemClickListener { pos, _ ->
            adapter_vacation!!.setCurrentPosition(
                pos
            )
        }
    }

    override fun onClick(view: View?) {
        when(view!!.id) {
            R.id.btn_anwser_question -> {
                if (ClickUtils.isDoubleClick(btn_anwser_question)) {
                    return
                }
                if(!isLogin) {
                    startActivity(Intent(this.context, LoginActivity::class.java))
                } else {

                   if(radio_random.isChecked) {
                       question_pattern = "随机模式"
                   }
                    if(radio_knowledge.isChecked) {
                       question_pattern = "知识点模式"
                   }
                    if(radio_challenge.isChecked) {
                       question_pattern = "挑战模式"
                   }

                    val intent = Intent(this.context,AnswerQuestionActivity::class.java)
                    val bundle = Bundle()
                    bundle.putString("question_pattern",question_pattern)
                    intent.putExtras(bundle)
                    startActivity(intent)
                }
            }
        }
    }
}